package map.tools.plateform;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import uimigration.Plateform.PlateformPackage;
import uimigration.mappingmodel.MappingTable;
import uimigration.mappingmodel.MappingmodelPackage;

public class MappingTableSerializer {
	
	public MappingTable load(File file) {
	    ResourceSet rs = new ResourceSetImpl(); 
	    Resource.Factory.Registry registry = rs.getResourceFactoryRegistry();
	    Map<String,Object> m = registry.getExtensionToFactoryMap();
	    m.put("xmi", new XMIResourceFactoryImpl());
	    rs.getPackageRegistry().put(MappingmodelPackage.eNS_URI, MappingmodelPackage.eINSTANCE);
	    URI uri = URI.createFileURI(file.getAbsolutePath());
	    Resource resource = rs.getResource(uri, true);
	    if (resource.isLoaded() && resource.getContents().size() > 0) {
	    	MappingTable devicem=(MappingTable) resource.getContents().get(0);	
	    	return devicem; 
	    }
	    return null;
	}
	 public void save(MappingTable mapping, File f,String fileName) {
		    ResourceSet rs = new ResourceSetImpl();
		    Resource.Factory.Registry registry = rs.getResourceFactoryRegistry();
		    Map<String,Object> m = registry.getExtensionToFactoryMap();
		    m.put("xmi", new XMIResourceFactoryImpl());
		    m.put("ecore", new EcoreResourceFactoryImpl());
		    rs.getPackageRegistry().put(MappingmodelPackage.eNS_URI, MappingmodelPackage.eINSTANCE);
		    Resource packageResource = rs.createResource(URI.createFileURI(fileName));
		    packageResource.getContents().add(PlateformPackage.eINSTANCE);
		    try { 
		      packageResource.load(null); 
		    } catch (IOException e1) { 
		      e1.printStackTrace(); 
		    }
		    URI uri = URI.createFileURI(f.getAbsolutePath());
		    Resource resource = rs.createResource(uri);
		    resource.getContents().add(mapping);
		    try {
		      HashMap<String, Boolean> options = new HashMap<String, Boolean>();
		      options.put("SCHEMA_LOCATION", Boolean.TRUE);
		      resource.save(options);
		    } catch (IOException e) { e.printStackTrace(); }
		  }
/*		 
	public void encryptedSave(Plateform api, File f, String fileName) {
		    ResourceSet rs = new ResourceSetImpl();
		    Resource.Factory.Registry registry = rs.getResourceFactoryRegistry();
		    Map<String,Object> m = registry.getExtensionToFactoryMap();
		    m.put("xmi", new  XMIResourceFactoryImpl() {
		      public Resource createResource(URI uri) {
			XMIResourceFactoryImpl resFactory = new XMIResourceFactoryImpl();
			XMIResource resource = (XMIResource) resFactory.createResource(uri);
			try {
			  resource.getDefaultLoadOptions().put(Resource.OPTION_CIPHER, new AESCipherImpl("foo"));
			  resource.getDefaultSaveOptions().put(Resource.OPTION_CIPHER, new AESCipherImpl("foo"));
			} catch (Exception e) {
			  e.printStackTrace();
			}
			return resource;
			}});
		    m.put("ecore", new EcoreResourceFactoryImpl());
		    rs.getPackageRegistry().put(PlateformPackage.eNS_URI, PlateformPackage.eINSTANCE);
		    Resource packageResource = rs.createResource(URI.createFileURI(fileName));
		    packageResource.getContents().add(PlateformPackage.eINSTANCE);
		    try { 
		      packageResource.load(null); 
		    } catch (IOException e1) { 
		      e1.printStackTrace(); 
		    }
		    URI uri = URI.createFileURI(f.getAbsolutePath());
		    Resource resource = rs.createResource(uri);
		    resource.getContents().add(api);
		    try {
		      HashMap<String, Boolean> options = new HashMap<String, Boolean>();
		      options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
		      resource.save(options);
		    } catch (IOException e) { e.printStackTrace(); }
		  }
*/


}
